/*
 * Copyright (c) 2007 The Chai Team
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

package com.google.projects.chai.testutil;

import com.google.projects.chai.util.ErrorCode;
import junit.framework.TestCase;
import org.apache.xmlrpc.XmlRpcException;

/**
 * Provides common functionality for testing XML-RPC calls.
 * @author dap
 */
public class XmlRpcTestCase extends TestCase {
        private TestClient client;
        
        public XmlRpcTestCase(String name) {
                super(name);
        }
        
        /** @return the client used to interact with the server */
        public TestClient getClient() {
                return (client);
        }
        
        /** Instantiates a server and client. */
        protected void setUp() throws Exception {
                client = new TestClient();
        }
        
        /**
         * Asserts that the given method fails with the given error when applied
         * to the given args.
         *
         * @param ec an error
         * @param method the name of an XML-RPC method to call
         * @param args arguments to the method
         */
        public void assertFails(ErrorCode ec, String method, Object... args) {
                String msg = method + " should have failed with " + ec;
                try {
                        client.execute(method, args);
                        assertFalse(msg + ", but succeeded instead", false);
                } catch (XmlRpcException e) {
                        // TODO we should be checking error codes here.
                        // we need to change the server to give the raw
                        // XmlRpcException that we're trying to throw. For now,
                        // we just check strings...ick.
                        msg += " but instead failed with " + e.getMessage();
                        assertTrue(msg, e.getMessage().endsWith(ec.getString()));
                }
        }
        
        /**
         * Executes the named method on the client with the given args.
         */
        public Object execute(String method, Object... args) throws 
            XmlRpcException {
                return (client.execute(method, args));
        }
}
